import { FMVCS } from "../../fgui/fmvc/FMVCS";
import { GlobalEvent } from "../global/Global";
import EventSystem from "../../core/EventSystem";
import PlayerCtrl from "../player/PlayerCtrl";
import { Platform } from "../../platform/Platform";

export default class HomeView extends FMVCS.FView {
    clickHandlers() {
        return [
            [this._views[`btnStart`], this.onBtnStart, this],
            [this._views[`btnShare`], this.onBtnShare, this],
            [this._views[`btnRank`], this.onBtnRank, this],
            [this._views[`btnJJC`], this.onBtnJJC, this],
            [this._views[`btnLocalMap`], this.onBtnLocalMap, this],
        ]
    }

    onOpenCallBack() {
        EventSystem.on(GlobalEvent.GameEventID.UserLoginSuc, this.onUserLoginSuccess, this);
        EventSystem.on(GlobalEvent.GameEventID.UpdateUserInfo, this.onUpdateUserInfo, this);
        this.onFlush();
    }

    onBtnStart() {
        EventSystem.emit(GlobalEvent.UIListenID.UI_BtnStart_Click);
    }

    onBtnShare() {
        Platform.instance.shareAppMessage();
    }

    onBtnRank() {
        Platform.instance.getUserInfo();
    }

    onBtnJJC() {
        EventSystem.emit(GlobalEvent.UIListenID.UI_BtnJJC_Click);
    }

    onBtnLocalMap() {
        EventSystem.emit(GlobalEvent.UIListenID.UI_MapLocalView_Open);
        this.onClose();
    }

    onUserLoginSuccess() {
        this.onFlush();
    }

    onUpdateUserInfo() {
        this.onFlush();
    }

    onFlush(type: string = `all`) {
        switch (type) {
            case `all`: {
                this._updateHeadIcon();
                this._updateLblNickName();
                break;
            }
        }
    }

    private _updateLblNickName() {
        this._views[`lblNickName`].asLabel.text = `昵称:` + PlayerCtrl.data.vo.nickName;
    }

    private _updateHeadIcon() {
        this._views[`headIcon`].asLoader.url = PlayerCtrl.data.vo.icon + `?a=a.jpg`;
    }
}
